package com.losdun.product.controller;

import com.losdun.common.annotation.Log;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.enums.BusinessType;
import com.losdun.common.utils.poi.ExcelUtil;
import com.losdun.product.controller.req.AttrRelatedReq;
import com.losdun.product.domain.Attribute;
import com.losdun.product.domain.AttributeRelated;
import com.losdun.product.service.IAttributeRelatedservice;
import com.losdun.product.service.IAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品属性Controller
 * 
 * @author sword.ye
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/product/attribute")
@Slf4j
public class AttributeController extends BaseController
{
    @Autowired
    private IAttributeService attributeService;
    @Autowired
    private IAttributeRelatedservice attributeRelatedService;

    /**
     * 查询产品属性列表
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:list')")
    @GetMapping("/list")
    public AjaxResult list(Attribute attribute)
    {
        List<Attribute> list = attributeService.selectAttributeList(attribute);
        return success(list);
    }

    /**
     * 导出产品属性列表
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:export')")
    @Log(title = "产品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attribute attribute)
    {
        List<Attribute> list = attributeService.selectAttributeList(attribute);
        ExcelUtil<Attribute> util = new ExcelUtil<Attribute>(Attribute.class);
        util.exportExcel(response, list, "产品属性数据");
    }

    /**
     * 获取产品属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attributeService.selectAttributeById(id));
    }

    /**
     * 新增产品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:add')")
    @Log(title = "产品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attribute attribute)
    {
        return toAjax(attributeService.insertAttribute(attribute));
    }

    /**
     * 修改产品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:edit')")
    @Log(title = "产品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attribute attribute)
    {
        return toAjax(attributeService.updateAttribute(attribute));
    }

    /**
     * 删除产品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:remove')")
    @Log(title = "产品属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attributeService.deleteAttributeByIds(ids));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('product:attribute:list')")
    @GetMapping("/tree")
    public AjaxResult attributeTree()
    {
        return success(attributeService.selectAttributeTree(new Attribute()));
    }

    @PostMapping("insertRelated")
    public AjaxResult insertAttrRelated(@RequestBody AttrRelatedReq req){
        try {
            attributeRelatedService.insertAttrRelated(req);
            return success();
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("插入属性关系失败,err:{}",e);
            }
            return error(e.getMessage());
        }

    }

    @RequestMapping("queryRelatedList")
    public AjaxResult queryRelatedList(@RequestParam Long attrId,@RequestParam String type){
        List<AttributeRelated> list = attributeRelatedService.queryRelatedList(attrId, type);
        return  success(list);
    }
}
